package org.jeecg.modules.demo.hd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.demo.hd.entity.AppMyProjectClickdetail;
import org.jeecg.modules.demo.hd.entity.AppMyProjectDetail;
import org.jeecg.modules.demo.hd.entity.AppProject;
import org.jeecg.modules.demo.hd.mapper.AppMyProjectClickdetailMapper;
import org.jeecg.modules.demo.hd.mapper.AppMyProjectDetailMapper;
import org.jeecg.modules.demo.hd.service.IAppMyProjectDetailService;
import org.jeecg.modules.demo.hd.service.IAppProjectService;
import org.jeecg.modules.demo.hd.vo.myProject.MyProjectDetailDayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: app_my_project_detail
 * @Author: jeecg-boot
 * @Date: 2025-04-18
 * @Version: V1.0
 */
@Service
public class AppMyProjectDetailServiceImpl extends ServiceImpl<AppMyProjectDetailMapper, AppMyProjectDetail> implements IAppMyProjectDetailService {

    @Resource
    private IAppProjectService projectService;

    @Resource
    private AppMyProjectClickdetailMapper appMyProjectClickdetailMapper;

    @Override
    public void updateUserProjectDetail(AppMyProjectClickdetail model) {
        // 查询项目每日需要完成数量，点击次数
        String projectId = model.getProjectId();
        AppProject _project = projectService.getProjectById(projectId);
        // 设置查询当天的数据范围
        LocalDateTime startOfDay = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endOfDay = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59).withNano(999999999);

        boolean finish = false;
        if (_project != null) {
            Integer clickCount = _project.getClickCount() == null ? 10 : _project.getClickCount();
            String _nowDate = DateUtils.getDate("yyyy-MM-dd");
            LambdaQueryWrapper<AppMyProjectClickdetail> clickdetailLambdaQueryWrapper = Wrappers.lambdaQuery();
            clickdetailLambdaQueryWrapper.eq(AppMyProjectClickdetail::getProjectId, projectId)
                    .eq(AppMyProjectClickdetail::getUserId, model.getUserId())
                    .between(AppMyProjectClickdetail::getCreateTime, startOfDay, endOfDay);
            int aLong = appMyProjectClickdetailMapper.selectCount(clickdetailLambdaQueryWrapper).intValue();
            if (clickCount < aLong) {
                finish = true;
            }
            // 查询是否已经创建当天完成记录
            LambdaQueryWrapper<AppMyProjectDetail> myProjectDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
            myProjectDetailLambdaQueryWrapper.eq(AppMyProjectDetail::getProjectId, projectId)
                    .eq(AppMyProjectDetail::getUserId, model.getUserId())
                    .between(AppMyProjectDetail::getCreateTime, startOfDay, endOfDay);
            AppMyProjectDetail detail = this.baseMapper.selectOne(myProjectDetailLambdaQueryWrapper);
            if (detail != null) {
                detail.setClickNum(aLong);
                detail.setIsFinish(finish ? 1 : 0);
                this.updateById(detail);
            } else {
                // 插入完成记录
                AppMyProjectDetail detailModel = new AppMyProjectDetail();
                detailModel.setProjectId(projectId);
                detailModel.setClickNum(aLong);
                detailModel.setUserId(model.getUserId());
                detailModel.setIsFinish(finish ? 1 : 0);
                this.save(detailModel);
            }
        }
    }

    @Override
    public List<MyProjectDetailDayVo> queryByProjectId(String userId, String projectId) {
        LocalDate startOfMonth = YearMonth.now().atDay(1); // 当前月份的第一天
        LocalDate endOfMonth = YearMonth.now().atEndOfMonth(); // 当前月份的最后一天

        LambdaQueryWrapper<AppMyProjectDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(AppMyProjectDetail::getUserId, userId)
                .eq(AppMyProjectDetail::getProjectId, projectId)
                .ge(AppMyProjectDetail::getCreateTime, startOfMonth) // 大于等于当前月份的第一天
                .le(AppMyProjectDetail::getCreateTime, endOfMonth); //小于等于当前月份的最后一天
        List<AppMyProjectDetail> appMyProjectDetails = this.baseMapper.selectList(lambdaQueryWrapper);
        List<MyProjectDetailDayVo> list = appMyProjectDetails.stream().map(item -> {
            MyProjectDetailDayVo v = new MyProjectDetailDayVo();
            v.setDay(item.getCreateTime());
            v.setClickNum(item.getClickNum());
            v.setIsFinish(item.getIsFinish());
            return v;
        }).collect(Collectors.toList());
        return list;
    }
}
